<?php
/**
 * File:          KValidatorElementUpload
 * Description:   -
 * Author:        Kovac "Kovacou'" Alexandre (www.kovacou.fr)
 * Last Update:   14/04/2011
 */

   abstract class KValidator_Element_Upload extends KValidatorElement {
      protected   $multiple,
                  $img,
                  $dir,
                  $auto,
                  $remove;
      
      
      public function auto ($use) {
         $this->auto = (bool) $use;
         
         return $this;
      }
      
      
      public function img ($use) {
        $this->img = (bool) $use;
        
        return $this;
      }
      
      
      public function ext () {
         $param = $this->_parameter("ext");
         $param->ext = func_get_args();
         
         return $this;
      }
      
      
      public function min ($x = null, $y = null) {
         if (false === $this->image)
            if (true === KV_DEBUG) throw new Exception ("KValidator_Element_Upload::min () - L'action des images est requise.");
            else return $this;
            
            
         $param = $this->_parameter ("min");
         $param->x = (int) $x;
         $param->y = (int) $y;
         
         return $this;
      }
      
      
      public function max ($x = null, $y = null) {
         if (false === $this->image)
            if (true === KV_DEBUG) throw new Exception ("KValidator_Element_Upload::max () - L'action des images est requise.");
            else return $this;
            
         $param = $this->_parameter ("max");
         $param->x = (int) $x;
         $param->y = (int) $y;
         
         return $this;
      }
      
      
      public function fix ($x = null, $y = null) {
         if (false === $this->image)
            if (true === KV_DEBUG) throw new Exception ("KValidator_Element_Upload::fix () - L'action des images est requise.");
            else return $this;
            
         $param = $this->_parameter ("fix");
         $param->x = (int) $x;
         $param->y = (int) $y;
         
         return $this;
      }
      
      
      public function size ($size, $unit = null) {
         $param = $this->_parameter ("size");
         $param->size = $this->_convertSize($size, $unit);
         
         return $this;
      }
      
      
      public function __construct ($name, $uniqid){
         $this->name      = $name;
         $this->uniqid    = $uniqid;
         $this->value     = KValidator::load ($this->uniqid)->getFile ($name);
         $this->required  = true;
         $this->dir       = null;
         $this->auto      = false;
      }
      
      
      public function valid () {
         if (false === isset ($this->value["error"]) || $this->value["error"] > 0)
            $error =  "Une erreur est survenue lors du tÃ©lÃ©chargement du fichier.";
         
         if (true === $this->required && true === empty ($this->value["tmp_name"]))
            $error =  "Vous devez sÃ©lectionner un fichier.";
         else
            foreach ($this->params as &$param) {
               $error = $param->valid ($this->value, array ($this->uniqid, $this->image));

                  if (isset ($error))
                     break;
            }
         
         if (isset ($error)) {
            KValidator::load ($this->uniqid)->setError ($this->name, $error);
            return false;
         }
         
         KValidator::load ($this->uniqid)->setUpload ($this->name, $this->value);
                 
         return true;
      }
      
      
      protected function _convertSize ($size, $unit) {
         $unit = strtolower($unit);
         $size = (int) $size;

         switch ($unit) {
            case 'ko':
            return $size * 1024^1;

            case 'mo':
            return $size * 1024^2;

            case 'go':
            return $size * 1024^3;

            case 'to':
            return $size * 1024^4;

            case 'po':
            return $size * 1024^5;

            default:
            return $size;
         }
      }
  
      
      protected function _parameter ($param) {
         return parent::_parameter("Upload", $param);
      }
   }
 
/**
 * End
 * Of
 * File
 */
?>